package api.server;

import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer6x;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
public class LuceneFlushCommitExample {


  public static void main(String[] args) {
    try {

      // 获取用户目录
      String userDirectory = System.getProperty("user.home");
      // 打印用户目录
      log.info("用户目录: " + userDirectory);
      // 获取文件分隔符
      String fileSeparator = System.getProperty("file.separator");
      // 打印文件分隔符
      log.info("文件分隔符: " + fileSeparator);


      String indexPath = userDirectory + fileSeparator + "Desktop" + fileSeparator + "linshilucenedata" + fileSeparator + "ix4";
      log.info("数据源目录 -> {} 索引生成目录 -> {}", "pathname", indexPath);

      System.out.println("start create index ... ");
      Path path = Paths.get(indexPath);
      Directory directory = FSDirectory.open(path);

      StandardAnalyzer analyzer = new StandardAnalyzer();
      IndexWriterConfig config = new IndexWriterConfig(analyzer);
      IndexWriter writer = new IndexWriter(directory, config);

      // 添加文档并flush，但不commit
      Document doc1 = new Document();
      doc1.add(new TextField("title", "Document 1", Field.Store.YES));
      writer.addDocument(doc1);
      writer.flush(); // 只执行flush

      // 关闭writer，模拟崩溃恢复后重新打开
      writer.close();

      analyzer = new StandardAnalyzer();
      config = new IndexWriterConfig(analyzer);
      writer = new IndexWriter(directory, config);

      // 添加另一个文档并commit
      Document doc2 = new Document();
      doc2.add(new TextField("title", "Document 2", Field.Store.YES));
      writer.addDocument(doc2);
      writer.commit(); // 执行commit，确保数据一致性

      // 打开 DirectoryReader 并进行搜索
      DirectoryReader reader = DirectoryReader.open(directory);
      IndexSearcher searcher = new IndexSearcher(reader);

      Query query = new MatchAllDocsQuery();
      TopDocs results = searcher.search(query, 10);
      System.out.println("Total Hits: " + results.totalHits.value);

      for (ScoreDoc scoreDoc : results.scoreDocs) {
        Document doc = searcher.doc(scoreDoc.doc);
        System.out.println("Document ID: " + scoreDoc.doc + " - " + doc.get("title"));
      }

      reader.close();
      writer.close();
      directory.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }


}
